package misc;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ComparatorTest {
    public static void main(String[] args) {
        Student a = new Student("a", 34);
        Student b = new Student("b", 26);
        Student c = new Student("c", 56);
        Student d = new Student("d", 16);
        List<Student> students = Arrays.asList(a, b, c, d);
        Collections.sort(students);
        students.forEach(System.out::println);
    }
}

class Student implements Comparable<Student> {

    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}
